<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetNotifyTemplates;

use App\Base\Resources\BaseAdminResource;
use App\Plugins\FcpApi\TweetPluginUsages\TweetPluginUsagesConfig;

class FsResourceDetail extends BaseAdminResource
{
    public function toArray($request)
    {
        // form 字段
        $formMap = FsConfig::FORM_FIELDS_MAP;
        $formMapFieldsArr = [];
        foreach ($formMap as $k => $dbField) {
            $formMapFieldsArr[$dbField] = $this->$dbField;
        }
        $lang_code = $request->header('lang-code', 'zh-CN');
        foreach (TweetPluginUsagesConfig::MULTILINGUAL_OPTION as $v) {
            // dd($lang_code);
            if ($v['nickname'] == $lang_code) {
                // dd($v);
                $lang_code = $v['key'];
            }
        }
        // 多语言模板
        $template_arr = json_decode($this->template, true);
        $template_name = [];
        foreach ($template_arr as $key => $v) {
            if ($lang_code == $key) {
                $template_name = $v;
            }
            // dd($key);
        }
        // 多语言模板注释
        $variable_desc_arr = json_decode($this->variable_desc, true);
        // dd($variable_desc_arr);
        $variable_desc_name = [];
        if ($variable_desc_name) {
            foreach ($variable_desc_arr as $key=>$v) {
                if ($lang_code == $key) {
                    $variable_desc_name = $v;
                }
            }
        }
        // 推送渠道
        $push_channel_arr = explode(',', $this->push_channel);
        // 禁止推送渠道
        $push_disable_arr = explode(',', $this->push_disable);
        // 默认字段
        $default = [
            'key'       => $this->id,
            'id'        => $this->id,
            'is_enable' => boolval($this->is_enable),
            'disabled'  => false,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

            'nickname'  => $this->nickname,
            'more_json'  => $this->more_json,
            'more_json_decode'  => json_decode($this->more_json, true),
            'push_channel_arr' => $push_channel_arr,
            'push_disable_arr' => $push_disable_arr,
            'template_decode' => json_decode($this->template, true),
            'variable_desc_decode' => json_decode($this->variable_desc, true),
            'template_name' => $template_name,
            'variable_desc_name' => $variable_desc_name,
        ];

        // 合并
        $arr = array_merge($formMapFieldsArr, $default);

        return $arr;
    }
}
